import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';

// import type {Visitor as VisitorType} from '@/models/visitor'
import { Global } from 'src/models/global';
import mutation from './module-example/mutations';
import userApi from 'src/api/user/index';
import { localStorage } from 'src/adapter/localstorage';
// 在Vue文件之外

export type LoginRequest = {
  account: string;
  password: string;
};

@Module<Global>({
  stateFactory: true,
  namespaced: true
})
export default class GlobalStateModule extends VuexModule {
  account: string = '';

  profile: any = {
    name: '',
    avatar: ''
  };

  name: string = '';
  //加载失败默认图片
  password: string = 'src/assets/images/errorImg2.png';

  access_token: string = '';

  refresh_token: string = '';

  @Mutation
  SET_ACCOUNT(account: string) {
    this.account = account;
  }

  @Mutation
  SET_PROFILE(profile: any) {
    this.profile = profile;
  }

  @Mutation
  SET_PASSWORD(password: string) {
    this.password = password;
  }
  @Mutation
  SET_ACCESS_TOKEN(access_token: string) {
    this.access_token = access_token;
  }
  @Mutation
  SET_RFRESH_TOKEN(refresh_token: string) {
    this.refresh_token = refresh_token;
  }

  get userProfile() {
    return this.profile;
  }
  get accessToken(): any {
    return this.access_token;
  }

  get refreshToken(): any {
    return this.refreshToken;
  }

  @Action({ rawError: true })
  async login({ account, password }: LoginRequest): Promise<any> {
    const res = await userApi.login(account, password);
    return new Promise((resolve, reject) => {
      if (res.success) {
        const { access_token, refresh_token } = res.data as any;
        this.context.commit('SET_ACCESS_TOKEN', access_token.token);
        localStorage.setItem('ACCESS_TOKEN', access_token.token);
        this.context.commit('SET_RFRESH_TOKEN', refresh_token.token);
        localStorage.setItem('REFRESH_TOKEN', refresh_token.token);
        this.context.dispatch('getUserProfile');
        resolve(res.data);
      } else {
        // reject(res);
      }
    });
  }

  @Action({ rawError: true })
  async oauthLogin(data: any) {
    const { access_token, refresh_token } = data;
    this.context.commit('SET_ACCESS_TOKEN', access_token.token);
    localStorage.setItem('ACCESS_TOKEN', access_token.token);
    this.context.commit('SET_RFRESH_TOKEN', refresh_token.token);
    localStorage.setItem('REFRESH_TOKEN', refresh_token.token);
    await this.context.dispatch('getUserProfile');
  }
  @Action({ rawError: true })
  async getUserProfile() {
    debugger;
    const res = await userApi.getProfile();
    return new Promise((resolve, reject) => {
      if (res.success) {
        const profile = res.data;
        this.context.commit('SET_PROFILE', profile);
        localStorage.setItem('PROFILE', profile);
        resolve(res.data);
      } else {
        console.error(res.msg);
        reject(false);
      }
    });
  }

  @Action({ rawError: true })
  async logout() {
    this.context.commit('SET_ACCESS_TOKEN', '');
    localStorage.removeItem('ACCESS_TOKEN');
    this.context.commit('SET_RFRESH_TOKEN', '');
    localStorage.removeItem('REFRESH_TOKEN');
    this.context.commit('SET_PROFILE', {});
    localStorage.removeItem('PROFILE');
  }
}
